package com.example.administrator.hqq.adapter.discover;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.util.Util;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.View.comment.CommentBean;
import com.example.administrator.hqq.View.comment.CommentTextView;
import com.example.administrator.hqq.View.comment.UserBean;
import com.example.administrator.hqq.bean.post.CommentDetailsBean;
import com.example.administrator.hqq.utils.TimeUtils;
import com.example.administrator.hqq.utils.transform.GlideCircleTransform;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/22 15:34
 * @remark:
 */
public class CommentDetailsAdapter extends RecyclerView.Adapter<CommentDetailsAdapter.ViewHolder> {

    private Context context;
    private List<CommentDetailsBean.DataBean.DiscussionBean> data;
    private onClikeListener onClike;
    public CommentDetailsAdapter(Context context, List<CommentDetailsBean.DataBean.DiscussionBean> data) {
        this.context = context;
        this.data = data;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.comment_details_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        CommentBean commentBean = new CommentBean();
        commentBean.setComment(data.get(position).getText());
        if (data.get(position).getType()==1)
        {
            UserBean replyUser = new UserBean();
            replyUser.setUserName("@"+data.get(position).getTarget_user_nickname());
            commentBean.setReplyUser(replyUser);
        }
        holder.commentContext.setText(commentBean);
        holder.userName.setText(data.get(position).getUser_nickname());
        if (Util.isOnMainThread())
        Glide.with(context).load(data.get(position).getUser_portrait_url())
                .centerCrop().transform(new CenterCrop(context),new GlideCircleTransform(context))
                .into(holder.userPic);
        holder.commentGiveLike.setSelected(data.get(position).getLike_state()==0?false:true);
        holder.likeNumber.setText(data.get(position).getLike_amount()==0?"":data.get(position).getLike_amount()+"");
        holder.commentTime.setText(TimeUtils.getTimeString(data.get(position).getTime()));
        holder.likeAuto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClike!=null)
                onClike.onlike(position,holder.commentGiveLike);
            }
        });
        holder.inform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClike!=null)
                onClike.onReply(position);
            }
        });
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClike!=null)
                    onClike.comment(position);

            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.user_pic)
        ImageView userPic;
        @BindView(R.id.user_name)
        TextView userName;
        @BindView(R.id.comment_context)
        CommentTextView commentContext;
        @BindView(R.id.inform)
        AutoRelativeLayout inform;
        @BindView(R.id.comment_time)
        TextView commentTime;
        @BindView(R.id.comment_give_like)
        ImageView commentGiveLike;
        @BindView(R.id.post_disagree)
        ImageView postDisagree;
        @BindView(R.id.auto2)
        AutoLinearLayout auto2;
        @BindView(R.id.like_auto)
        AutoLinearLayout likeAuto;
        @BindView(R.id.view1)
        View view1;
        @BindView(R.id.like_number)
        TextView likeNumber;
        View view;
        @BindView(R.id.examine_forum)
        TextView examineForum;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            view=itemView;
        }
    }

    public void setOnClike(onClikeListener onClike) {
        this.onClike = onClike;
    }

    public interface onClikeListener{
        void onlike(int position,ImageView imageView);
        void onReply(int posttion);
        void comment(int posttion);
    }
}
